﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;


namespace Warhammer40K_Simulator.view
{
    /// <summary>
    /// Interaction logic for elementlist.xaml
    /// </summary>
    public partial class elementlist : UserControl
    {
        public static readonly DependencyProperty titleproperty = DependencyProperty.Register("title"
            , typeof(String)
            , typeof(view.elementlist)
            , new PropertyMetadata(title_PropertyChanged));

        public String Title
        {
            get { return (String)GetValue(titleproperty); }
            set { SetValue(titleproperty, value); }
        }

        public String Bullet { get; set; }

        protected static void title_PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
        }

        public elementlist()
        {
            InitializeComponent();
            Bullet = "* ";
            this.DataContextChanged +=new DependencyPropertyChangedEventHandler(elementlist_DataContextChanged);
        }

        void elementlist_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            stackpanel_elements.Children.Clear();
            ObservableCollection<model.element> elements = e.NewValue as ObservableCollection<model.element>;

            if (elements != null && elements.Count > 0)
            {
                if (Title != null && Title != "")
                {
                    textblock_label.Visibility = Visibility.Visible;
                }
                stackpanel_elements.Visibility = Visibility.Visible;

                foreach (model.element ce in elements)
                {
                    bullettext ui = new bullettext();
                    ui.Bullet = Bullet;
                    if (ce.Count > 0)
                    {                        
                        ui.Text = ce.Count + " " + ce.Label + ((ce.Count > 1) ? "s" : "");
                    }
                    else
                    {
                        ui.Text = ce.Label;
                    }
                    stackpanel_elements.Children.Add(ui);

                    if (ce.Children != null && ce.Children.Count > 0)
                    {
                        elementlist eui = new elementlist();
                        eui.Bullet = "- ";
                        eui.DataContext = ce.Children;
                        ui.Children.Add(eui);
                    }
                    
                }
            }
            else
            {
                textblock_label.Visibility = Visibility.Collapsed;
                stackpanel_elements.Visibility = Visibility.Collapsed;
            }
        }
    }
}
